package com.lphj.datainterface.handle;

import com.lphj.datainterface.component.Config.ReadOtherConfig;
import com.lphj.datainterface.entity.*;
import com.lphj.datainterface.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class TGoodsSheetHandle {

    @Autowired
    ReadOtherConfig readOtherConfig;

    @Autowired
    TGoodsSheetService tGoodsSheetService;

    @Autowired
    TGoodsSheet_01Service tGoodsSheet_01Service;

    @Autowired
    TGoodsSheet_02Service tGoodsSheet_02Service;

    @Autowired
    TCustomerStoreService tCustomerStoreService;

    @Autowired
    TCumGoodsService tCumGoodsService;

    @Transactional
    public int addTGoodsSheet(List<TGoods> tGoodsList) {
        int count = 0;
        //清空tGoodsSheet
        tGoodsSheetService.delTGoodsSheet();
        TGoodsSheet tGoodsSheet = new TGoodsSheet();
        tGoodsSheet.setsPaperNO(OrderIdHandle.getOrderId(1 + "",new Date()));
        tGoodsSheet.setnVendorID(1);
        tGoodsSheet.setnTag(6);
        tGoodsSheet.setsCreateUser("管理员");
        tGoodsSheet.setsConfirmUser("管理员");
        tGoodsSheet.setnFlag(0);
        tGoodsSheetService.addTGoodsSheet(tGoodsSheet);

        //清空tGoodsSheet_01
        tGoodsSheet_01Service.delTGoodsSheet_01();
        TGoodsSheet_01 tGoodsSheet_01 = new TGoodsSheet_01();
        tGoodsSheet_01.setsPaperNO(tGoodsSheet.getsPaperNO());
        tGoodsSheet_01.setsStoreNO(readOtherConfig.getStoreNO());
        tGoodsSheet_01Service.addTGoodsSheet_01(tGoodsSheet_01);

        //清空tGoodsSheet_02
        tGoodsSheet_02Service.delTGoodsSheet_02();
        for(int i=0;i<tGoodsList.size();i++) {
            TGoods tGoods = tGoodsList.get(i);
            TGoodsSheet_02 tGoodsSheet_02 = new TGoodsSheet_02();
            tGoodsSheet_02.setsPaperNO(tGoodsSheet.getsPaperNO());
            tGoodsSheet_02.setnItem(i);
            tGoodsSheet_02.setnGoodsID(tGoods.getGoodsID());
            tGoodsSheet_02.setnSalePrice(tGoods.getSalePrice());
            tGoodsSheet_02.setnVipPrice(tGoods.getVipPrice());
            tGoodsSheet_02.setsSaleTaxID(tGoods.getSaleTaxID());
            tGoodsSheet_02.setsSaleTax(tGoods.getSaleTax());
            tGoodsSheet_02.setnSaleTaxPct(tGoods.getSaleTaxPct());
            tGoodsSheet_02.setsMainBarcode(tGoods.getMainBarcode());
            tGoodsSheet_02.setnShelfLife(tGoods.getShelfLife());
            tGoodsSheet_02.setnMainVendorID(tGoods.getMainVendorID());
            tGoodsSheet_02.setnSafeStockDay(tGoods.getSafeStockDay());
            tGoodsSheet_02.setnOPLM(tGoods.getOPLM());
            tGoodsSheet_02.setnCaseUnits(tGoods.getCaseUnits());
            tGoodsSheet_02.setnBuyPrice(tGoods.getBuyPrice());
            tGoodsSheet_02.setnRatio(tGoods.getRatio());
            tGoodsSheet_02.setnGiftGoodsID(tGoods.getGiftGoodsID());
            tGoodsSheet_02.setnGiftBase(tGoods.getGiftBase());
            tGoodsSheet_02.setnGiftQty(tGoods.getGiftQty());
            tGoodsSheet_02.setsBuyTaxID(tGoods.getBuyTaxID());
            tGoodsSheet_02.setsBuyTax(tGoods.getBuyTax());
            tGoodsSheet_02.setnBuyTaxPct(tGoods.getBuyTaxPct());
            tGoodsSheet_02.setnTag(646);
            tGoodsSheet_02.setnMinOrderQty(tGoods.getMinOrderQty());
            tGoodsSheet_02Service.addTGoodsSheet_02(tGoodsSheet_02);

            //获取所有客户编码
            List<String> cumNOList = tCustomerStoreService.getCustomerCumNO();

            //清空tCumGoods
            tCumGoodsService.delTCumGoods();
            //循环添加每个客户的商品默认售价
            for(String s : cumNOList) {
                //添加商品信息到TCumGoods
                TCumGoods tCumGoods = new TCumGoods();
                tCumGoods.setsStoreNo(readOtherConfig.getStoreNO());
                tCumGoods.setsCumNO(s);
                tCumGoods.setnGoodsID(tGoods.getGoodsID());
                tCumGoods.setnNormalPrice(tGoods.getSalePrice());
                tCumGoods.setnSalePrice(tGoods.getSalePrice());
                tCumGoods.setnTag(0);
                tCumGoods.setnTag1(0);
                tCumGoods.setsSheetID(tGoodsSheet.getsPaperNO());
                tCumGoods.setnOrderCount(0);
                tCumGoodsService.addTCumGoods(tCumGoods);
            }
        }
        return count;

    }
}
